<div class="layuimini-container">
    <div class="layuimini-main">

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加</button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除</button>
            </div>
        </script>
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-xs" lay-event="authorize">授权</a>
            <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="delete">删除</a>
        </script>

    </div>
</div>

<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        table.render({
            elem: '#currentTableId',
            url: '{:url("role/index")}',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox"},
                {field: 'id', width: 80, title: 'ID'},
                {field: 'title', minWidth: 80, title: '角色名称'},
                {field: 'remark', minWidth: 80, title: '备注信息'},
                {
                    field: 'status', title: '状态', width: 85, templet: function (d) {
                        if (d.status==1) {
                            return  '<span style="color:#009688;">有效</span>';
                        } else {
                            return  '<span style="color: #FF5722;">无效</span>';
                        }
                    }
                },
                {field: 'create_time', minWidth: 80, title: '创建时间', search: 'range'},
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            skin: 'line'
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            var event = obj.event;
            if (event === 'add') {  // 监听添加操作
                openDetail('新增','{:url("role/add")}');
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                var ids = [];
                for (var i=0;i<data.length;i++){
                    ids.push(data[i]['id']);
                }
                delItem('{:url("role/del")}',ids);
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'edit') {
                var url = '{:url("role/edit",["id"=>"ID"])}';
                url = url.replace("ID",data.id);
                openDetail('修改',url);
            } else if (layEvent === 'delete') {
                delItem('{:url("role/del")}',data.id);
            }else if(layEvent==='authorize'){
                var url = '{:url("role/authorize",["id"=>"ID"])}';
                url = url.replace("ID",data.id);
                openDetail('授权',url);
            }
        });
    });
</script>